/////////////////////////////////////////////////////////////////////////////////
//
//
//
//
/////////////////////////////////////////////////////////////////////////////////
#include <Stdio.h>
#include "CommonClass.h"
#include "LessonX.h"
////////////////////////////////////////////////////////////////////////////////
//
//
CGameMain		g_GameMain;

//==============================================================================
//
// ����ĳ�������Ϊ��GameMainLoop����Ϊ��ѭ��������������ÿ֡ˢ����Ļͼ��֮�󣬶��ᱻ����һ�Ρ�

//==============================================================================
//
// ���캯��
CGameMain::CGameMain()
{
	m_iGameState			=	0;
	m_pFishHeader =	NULL;
	m_pMyFish =	NULL;

	bgSound = new CSound("fishmusic.wav",true,1); //��������

	m_fCreateNormalTime	=	0.5f;  // ��ͨ���һ�γ��ֵ�ʱ����
	m_fCreateSpecialTime =	5.0f;  // �������һ�γ��ֵ�ʱ����

	m_fWorldLeft =	0.f;
	m_fWorldRight =	10.f;
	m_fWorldTop	=	0.f;
	m_fWorldBottom	=	10.f;

	m_pLevelText = new CTextSprite("LevelText");		// ��ʾ�ȼ�������
	m_pHPText = new CTextSprite("HPText");			// ��ʾHP������
	m_pBeginSprite = new CTextSprite("kaishi");			// ��ʾ��ʼ��Ϸ������ͼƬ
	m_pBeginSpritePlus = new CTextSprite("kaishibiaoti");	//��ʾ��ʼ��Ϸ�ı���ͼƬ
	m_pJieShuSprite = new CTextSprite("jieshu");		//��ʾ��Ϸ�����ı�־ͼƬ



	//�������
	m_fCreatePowerUpTime = 10.0f;
    m_fSpeedUpTime = 0.0f;
    m_fGodModeTime = 0.0f;
    m_bIsGodMode = false;


    //��ͼ�л�
    m_bNeedReinit = true;  // ��ʼʱ��Ҫ��ʼ��

    m_fCreateJianYouTime = 0.0f;
    m_fCreateJianZuoTime = 0.0f;
    m_fJianInterval = 1.0f; // 设置箭生成间隔为1秒
}
//==============================================================================
//
// ��������
CGameMain::~CGameMain()
{
}
// ��ȡ�����������
const char *CGameMain::GetControlFishName()
{
	return ( m_pMyFish ? m_pMyFish->GetName() : "" );
}

// ��Ϸ�Ƿ�Ӯ��--��ĵȼ��ﵽ4��, ����HP > 6666
bool CGameMain::IsGameWin()
{
	return ( m_pMyFish ? (m_pMyFish->GetLevel() >= 4 && m_pMyFish->GetHp() > 6666) : false );
}

// �Ƿ����� -- ��������0��
bool CGameMain::IsGameLost()
{
	return ( m_pMyFish ? (m_pMyFish->GetLevel() <= 0) : false );
}


// 新增CMyFish2类实现
CMyFish2::CMyFish2(const char *szName, const int iHp, const int iLevel)
    : CGameSprite(szName)
{
    m_iHp = iHp;
    m_iLevel = iLevel;

    m_fVelocityLeft = 0.f;
    m_fVelocityRight = 0.f;
    m_fVelocityUp = 0.f;
    m_fVelocityDown = 0.f;

    SetSpriteScale(0.25f);
    SetSpriteWorldLimit(WORLD_LIMIT_STICKY,
        g_GameMain.GetWorldLeft() - 2.f, g_GameMain.GetWorldTop() - 2.f,
        g_GameMain.GetWorldRight() + 2.f, g_GameMain.GetWorldBottom() + 2.f);
}

CMyFish2::~CMyFish2()
{
}

void CMyFish2::OnMove(bool bKeyDown, int iKey)
{
    if(bKeyDown)
    {
        switch(iKey)
        {
        case KEY_A: m_fVelocityLeft = 30.f; break;
        case KEY_D: m_fVelocityRight = 30.f; break;
        case KEY_W: m_fVelocityUp = 15.f; break;
        case KEY_S: m_fVelocityDown = 15.f; break;
        }
    }
    else
    {
        switch(iKey)
        {
        case KEY_A: m_fVelocityLeft = 0.f; break;
        case KEY_D: m_fVelocityRight = 0.f; break;
        case KEY_W: m_fVelocityUp = 0.f; break;
        case KEY_S: m_fVelocityDown = 0.f; break;
        }
    }

    float fVelX = m_fVelocityRight - m_fVelocityLeft;
    float fVelY = m_fVelocityDown - m_fVelocityUp;
    SetSpriteLinearVelocity(fVelX, fVelY);

    if(fVelX > 0.0001f) SetSpriteFlipX(false);
    else if(fVelX < -0.0001f) SetSpriteFlipX(true);
}







//==============================================================================
//
// ��Ϸ��ѭ�����˺���������ͣ�ĵ��ã�����ÿˢ��һ����Ļ���˺�����������һ��
// ���Դ�����Ϸ�Ŀ�ʼ�������С������ȸ���״̬.
// ��������fDeltaTime : �ϴε��ñ��������˴ε��ñ�������ʱ��������λ����
void CGameMain::GameMainLoop( float	fDeltaTime )
{

    // ������ȷ��UI״̬��ȷ
    if (m_iGameState == 0 && !m_pBeginSprite->IsSpriteVisible())
    {
        ResetUIState();
    }




	switch( GetGameState() )
	{
		// ��ʼ����Ϸ�������һ���������
	case 1:
		{
			GameInit();
			SetGameState(2); // ��ʼ��֮�󣬽���Ϸ״̬����Ϊ������
		}
		break;

		// ��Ϸ�����У�����������Ϸ�߼�
	case 2:
		{
				// �ж���Ӯ
			if( IsGameWin() || IsGameLost() )
			{
				// ��Ϸ������������Ϸ���㺯����������Ϸ״̬�޸�Ϊ����״̬6
				SetGameState(0);
				GameEnd();
			}
			else // ��Ϸδ������������Ϸ
			{
				GameRun( fDeltaTime );
			}
		}
		break;

		// ��Ϸ����/�ȴ����ո����ʼ
	case 0:
	default:
		break;
	};
}
//=============================================================================
//
// ÿ�ֿ�ʼǰ���г�ʼ���������һ���������
void CGameMain::GameInit()
{


    // ȷ��ֻ����Ҫʱ��ʼ��
    if (!m_bNeedReinit) return;


    m_fWorldLeft =	CSystem::GetScreenLeft();
	m_fWorldTop	= CSystem::GetScreenTop();
	m_fWorldRight =	CSystem::GetScreenRight();
	m_fWorldBottom = CSystem::GetScreenBottom();

	bgSound->PlaySound();

	// ������ҿ��Ƶ���. ��������ȼ�Ϊ1��HPΪ10. ��4������HP����һ��������ʱ��������ÿ����ʾ��ͼƬ��С����
	if( NULL == m_pMyFish )
	{
		// ��������ʵ��
		m_pMyFish =	new CMyFish("kongzhiyu", 10, 1);
	}
	else
	{
		m_pMyFish->SetLevel( 1 );
		m_pMyFish->SetHp( 10 );

		// �������ģ�ʹ�СΪ1/4
		m_pMyFish->SetSpriteScale( 0.25f );
	}

	// ���µ�ǰ�ȼ�/HP��ʾ
	m_pLevelText->SetTextValue( 1 );
	m_pHPText->SetTextValue( 0 );
	// ���� "�ո�ʼ��Ϸ" �����ʾͼƬ
	m_pBeginSprite->SetSpriteVisible( false );
	//���ش�������ͼƬ
	m_pBeginSpritePlus->SetSpriteVisible( false );
	// ���� "��Ϸ����" �����ʾͼƬ
	m_pJieShuSprite->SetSpriteVisible( false );

	// 新增：初始化第二个控制鱼（使用地图名称判断）
    static bool bLevel2Checked = false;
    static bool bIsLevel2 = false;

    if(!bLevel2Checked)
    {
        CSprite* pTest = new CSprite("level2_test");
        bIsLevel2 = (pTest != NULL);
        delete pTest;
        bLevel2Checked = true;
    }

    if(bIsLevel2 && NULL == m_pMyFish2)
    {
        m_pMyFish2 = new CMyFish2("kongzhiyu2", 10, 1);
        m_pMyFish2->CloneSprite("kongzhiyu2_muban"); // 假设这是模板精灵名
    }



    // 确保baoxiang1有碰撞属性（仅在level2.t2d）
    if(m_pMyFish2) // 如果存在第二个控制鱼，说明是level2
    {
        // 确保kongzhiyu2有碰撞属性
        m_pMyFish2->SetSpriteCollisionSend(true);
        m_pMyFish2->SetSpriteCollisionReceive(true);
        m_pMyFish2->SetSpriteCollisionResponse(COL_RESPONSE_CUSTOM);

        // 设置所有baoxiang1的碰撞属性
        FishList* pFish = m_pFishHeader;
        while(pFish)
        {
            if(strstr(pFish->pPtr->GetName(), "baoxiang1"))
            {
                pFish->pPtr->SetSpriteCollisionSend(true);
                pFish->pPtr->SetSpriteCollisionReceive(true);
                pFish->pPtr->SetSpriteCollisionResponse(COL_RESPONSE_CUSTOM);
                //pFish->pPtr->SetSpriteLayer(10);
            }
            pFish = pFish->pNext;
        }
    }

}
//=============================================================================
//
// ÿ����Ϸ������
void CGameMain::GameRun( float fDeltaTime )
{


	char szName[128];

	// ������ͨ��CNormalFish
	// ��Ļÿˢ��һ�Σ���ͨ����ֵ�ʱ�����Ӧ����fDeltaTime
	SetCreateNormalTime( GetCreateNormalTime() - fDeltaTime );
	//
	if( GetCreateNormalTime() <= 0.f )
	{
		// ���һ��ʱ�䣬��Ϊ�´δ������ʱ�䡣�Ȼ��������Ȼ����ת����С����
		int	iNextTime	=	CSystem::RandomRange( 0, 200 );
		SetCreateNormalTime( (float)iNextTime / 100.f );

		// ����CGameSprite��̬��Ա
		sprintf( szName, "putongyu_%d", CGameSprite::m_iCreatedFishCount );
		CGameSprite::m_iCreatedFishCount++;

		CNormalFish	*pFish	=	NULL;
		int	iRandom	= CSystem::RandomRange( 1, 100 );
		// ���ǿ��Ƶ��㣬1����ʱ��ֻ����1��2������������
		if( 1 == GetControlFish()->GetLevel() )
		{
			// 65%�ļ��ʳ���1���㣬35�ļ��ʳ���2����
			if( iRandom < 35 )
			{
				pFish	=	new CNormalFish( szName, 50, 2 );
				// ������ʾ��Sprite
				pFish->CloneSprite( "yu_muban2" );
			}
			else
			{
				pFish	=	new CNormalFish( szName, 10, 1 );
				// ������ʾ��Sprite
				pFish->CloneSprite( "yu_muban1" );
			}
		}
		// �����Ƶ���Ϊ2��ʱ��
		else if( 2 == GetControlFish()->GetLevel() )
		{
			//�ȼ�3Ϊ20%���ȼ�2Ϊ50%���ȼ�1Ϊ30%
			if( iRandom < 20 )
			{
				pFish	=	new CNormalFish( szName, 100, 3 );
				// ������ʾ��Sprite
				pFish->CloneSprite( "yu_muban3" );
			}
			else if( iRandom < 70 )
			{
				pFish	=	new CNormalFish( szName, 50, 2 );
				// ������ʾ��Sprite
				pFish->CloneSprite( "yu_muban2" );
			}
			else
			{
				pFish	=	new CNormalFish( szName, 10, 1 );
				// ������ʾ��Sprite
				pFish->CloneSprite( "yu_muban1" );
			}
		}
		// �����Ƶ���ĵȼ�Ϊ���ڵ���3ʱ��
		else
		{
			//�ȼ�4Ϊ20%���ȼ�3Ϊ50%���ȼ�2Ϊ20%���ȼ�1Ϊ10%
			if( iRandom < 20 )
			{
				pFish	=	new CNormalFish( szName, 1000, 4 );
				// ������ʾ��Sprite
				pFish->CloneSprite( "yu_muban4" );
			}
			else if( iRandom < 70 )
			{
				pFish	=	new CNormalFish( szName, 100, 3 );
				// ������ʾ��Sprite
				pFish->CloneSprite( "yu_muban3" );
			}
			else if( iRandom < 90 )
			{
				pFish	=	new CNormalFish( szName, 50, 2 );
				// ������ʾ��Sprite
				pFish->CloneSprite( "yu_muban2" );
			}
			else
			{
				pFish	=	new CNormalFish( szName, 10, 1 );
				// ������ʾ��Sprite
				pFish->CloneSprite( "yu_muban1" );
			}
		}

		if( NULL != pFish )
		{
			// ���ӵ�������
			AddFish( pFish );

			// ����������꼰�ٶ�(�ڳ����Ҳ࿴�����ĵط���ʼ�γ������������������Ҳ��ټ�һ�����,������Ҫ�����X����ת����)
			iRandom	=	CSystem::RandomRange( (int)GetWorldTop(), (int)GetWorldBottom() );
			pFish->SetSpritePosition( GetWorldRight() + 10.f, (float)iRandom );
			pFish->SetSpriteFlipX( true );

			// ����ٶ�
			iRandom	=	CSystem::RandomRange( 10, 25 );
			pFish->SetSpriteLinearVelocityX( -(float)iRandom );

			// ����߽�����
			pFish->SetSpriteWorldLimit( WORLD_LIMIT_NULL, GetWorldLeft() - 10.f, GetWorldTop(), GetWorldRight() + 20.f, GetWorldBottom() );
		}
	}

	// ����������CSpecialFish
	SetCreateSpecialTime( GetCreateSpecialTime() - fDeltaTime );
	if( GetCreateSpecialTime() <= 0.f )
	{
		// ���һ��ʱ�䣬��Ϊ�´δ������ʱ�䡣�Ȼ��������Ȼ����ת����С����
		int	iNextTime	=	CSystem::RandomRange( 500, 1000 );
		SetCreateSpecialTime( (float)iNextTime / 100.f );

		// ����CGameSprite��̬��Ա
		sprintf( szName, "putongyu_%d", CGameSprite::m_iCreatedFishCount );
		CGameSprite::m_iCreatedFishCount++;
		// HPֵ��������ҿ��Ƶ���ĵȼ����������Ϊ1����ʱ�򣬸���������HP 10��Ϊ2����ʱ�򣬸���HP 100,3����ʱ�򣬸���HP 500
		int	iFishHP	=	10;
		if( GetControlFish()->GetLevel() == 1 )
			iFishHP = 10;
		else if( GetControlFish()->GetLevel() == 2 )
			iFishHP = 100;
		else
			iFishHP = 500;

		CSpecialFish *pFish	=	new CSpecialFish( szName, iFishHP );
		pFish->CloneSprite( "yu_muban5" );

		AddFish( pFish );

		int iRandom	=	CSystem::RandomRange( (int)GetWorldTop(), (int)GetWorldBottom() );
		pFish->SetSpritePosition( GetWorldRight() + 10.f, (float)iRandom );
		pFish->SetSpriteFlipX( true );

		// ����ٶ�
		iRandom	=	CSystem::RandomRange( 10, 25 );
		pFish->SetSpriteLinearVelocityX( -(float)iRandom );

		// ����߽�����
		pFish->SetSpriteWorldLimit( WORLD_LIMIT_NULL, GetWorldLeft() - 10.f, GetWorldTop(), GetWorldRight() + 20.f, GetWorldBottom() );
	}

	// ���µ�ǰ�ȼ�/HP��ʾ
	m_pLevelText->SetTextValue( GetControlFish()->GetLevel() );
	m_pHPText->SetTextValue( GetControlFish()->GetHp() );


	//�����������
	CreatePowerUps(fDeltaTime);
    UpdatePowerUpEffects(fDeltaTime);

    CreateJianYou(fDeltaTime);
    CreateJianZuo(fDeltaTime);

}



// ��������ʵ��
void CGameMain::CreatePowerUps(float fDeltaTime) {
    m_fCreatePowerUpTime -= fDeltaTime;

    if (m_fCreatePowerUpTime <= 0.f) {
        // �������1-3����ǿ����
        int iCount = CSystem::RandomRange(1, 4);
        for (int i = 0; i < iCount; ++i) {
            // ��������ֱ�Ӱ���������Ϣ
            int iType = CSystem::RandomRange(0, 3);
            char szName[128];

            switch (iType) {
                case 0: sprintf(szName, "jiaxie_powerup_%d", CGameSprite::m_iCreatedFishCount++); break;
                case 1: sprintf(szName, "jiasu_powerup_%d", CGameSprite::m_iCreatedFishCount++); break;
                case 2: sprintf(szName, "wudi_powerup_%d", CGameSprite::m_iCreatedFishCount++); break;
            }

            CGameSprite* pPowerUp = new CGameSprite(szName);

            // �����������ò�ͬ����
            switch (iType) {
                case 0: pPowerUp->CloneSprite("jiaxie"); break;
                case 1: pPowerUp->CloneSprite("jiasu"); break;
                case 2: pPowerUp->CloneSprite("wudi"); break;
            }

            // ����λ�ú��ٶ�...
            int iRandomY = CSystem::RandomRange((int)GetWorldTop(), (int)GetWorldBottom());
            pPowerUp->SetSpritePosition(GetWorldRight() + 10.f, (float)iRandomY);
            pPowerUp->SetSpriteFlipX(true);
            pPowerUp->SetSpriteLinearVelocityX(-15.f);

            // ����߽�����
            pPowerUp->SetSpriteWorldLimit(WORLD_LIMIT_NULL,
                GetWorldLeft() - 10.f, GetWorldTop(),
                GetWorldRight() + 20.f, GetWorldBottom());

            AddFish(pPowerUp);
        }

        m_fCreatePowerUpTime = (float)CSystem::RandomRange(10, 21);
    }
}

void CGameMain::UpdatePowerUpEffects(float fDeltaTime) {
    if (m_fSpeedUpTime > 0.f) {
        m_fSpeedUpTime -= fDeltaTime;
        if (m_fSpeedUpTime <= 0.f && m_pMyFish) {
            m_pMyFish->SetSpriteLinearVelocityX(30.f);
            m_pMyFish->SetSpriteLinearVelocityY(15.f);
        }
    }

    if (m_fGodModeTime > 0.f) {
        m_fGodModeTime -= fDeltaTime;
        if (m_fGodModeTime <= 0.f) {
            m_bIsGodMode = false;
        }
    }
}








//=============================================================================
//
// ������Ϸ����
void CGameMain::GameEnd()
{
    // �����Ƶ����ٶȹ��㣬������ڿո�ʼ��ʶ�����
	GetControlFish()->SetSpriteLinearVelocity( 0.f, 0.f );
	GetControlFish()->SetSpriteFlipX( false );
	GetControlFish()->SetSpritePosition( -10.f,3.f );

	// ɾ���Ͼֵ�������
	DeleteAllFish();

	//��Ϸ����ʱ���ֲ���ֹͣ
	bgSound -> StopSound();

	// ���µ�ǰ�ȼ�/HP��ʾ
	m_pLevelText->SetTextValue( 1 );
	m_pHPText->SetTextValue( 0 );
	// ��ʾ "��Ϸ����" �����ʾͼƬ
	m_pJieShuSprite->SetSpriteVisible( true );
	//��ʾ��������ͼƬ
	m_pBeginSpritePlus->SetSpriteVisible(true);
}
//==========================================================================
//
// ����ƶ�
// ���� fMouseX, fMouseY��Ϊ��굱ǰ����
void CGameMain::OnMouseMove( const float fMouseX, const float fMouseY )
{

}
//==========================================================================
//
// �����
// ���� iMouseType����갴��ֵ���� enum MouseTypes ����
// ���� fMouseX, fMouseY��Ϊ��굱ǰ����
void CGameMain::OnMouseClick( const int iMouseType, const float fMouseX, const float fMouseY )
{

}
//==========================================================================
//
// ��굯��
// ���� iMouseType����갴��ֵ���� enum MouseTypes ����
// ���� fMouseX, fMouseY��Ϊ��굱ǰ����
void CGameMain::OnMouseUp( const int iMouseType, const float fMouseX, const float fMouseY )
{

}
//==========================================================================
//
// ���̰���
// ���� iKey�������µļ���ֵ�� enum KeyCodes �궨��
// ���� iAltPress, iShiftPress��iCtrlPress�������ϵĹ��ܼ�Alt��Ctrl��Shift��ǰ�Ƿ�Ҳ���ڰ���״̬(0δ���£�1����)
void CGameMain::OnKeyDown( const int iKey, const bool bAltPress, const bool bShiftPress, const bool bCtrlPress )
{
    // ���¿ո���Ϸ��ʼ
	if( KEY_SPACE == iKey && 0 == GetGameState() )
	{
		SetGameState( 1 );
	}
	else if( 2 == GetGameState() )
	{
		m_pMyFish->OnMove(true,iKey);
		// 新增：第二个控制鱼的按键响应
        if(m_pMyFish2)
            ((CMyFish2*)m_pMyFish2)->OnMove(true, iKey);
	}
}
//==========================================================================
//
// ���̵���
// ���� iKey������ļ���ֵ�� enum KeyCodes �궨��
void CGameMain::OnKeyUp( const int iKey )
{
    if( 2 == GetGameState() )
	{
        m_pMyFish->OnMove(false,iKey);
        // 新增：第二个控制鱼的按键释放响应
        if(m_pMyFish2)
            ((CMyFish2*)m_pMyFish2)->OnMove(false, iKey);
	}
}
//===========================================================================
//
// �����뾫����ײ
// ���� szSrcName��������ײ�ľ�������
// ���� szTarName������ײ�ľ�������
void CGameMain::OnSpriteColSprite( const char *szSrcName, const char *szTarName )
{
    if( 2 != GetGameState() || NULL == GetControlFish() )
		return;

    // 新增：处理kongzhiyu2与baoxiang1的碰撞
    HandleKongzhiyu2Collision(szSrcName, szTarName);

    // ��һ�������ȴ���������ײ
    if (strstr(szTarName, "powerup_") || strstr(szSrcName, "powerup_")) {
        HandlePowerUpCollision(szSrcName, szTarName);
        return; // �������ֱ�ӷ���
    }

    // �ڶ�����������ͨ�����ײ


	// �ж��Ƿ�����ҵ������Ϸ�������ײ��
	if( strcmp( GetControlFishName(), szSrcName ) == 0 )
	{
		// ��ײĿ�겻����Ϸ����㣬����
		if( !strstr( szTarName, "putongyu" ) )
			return;

		// �ҵ�����Ķ���ʵ����������ײ��������
		CGameSprite	*pFish	=	GetFishFromName( szTarName );
		if( NULL != pFish )
		{
			pFish->OnColControlFish((CMyFish*)GetControlFish() );
			// ��ײ֮�󣬲���˭��˭��������Ϸ�����ɾ����
			DeleteFishFromName( szTarName );
		}
	}
	else if( strcmp( GetControlFishName(), szTarName ) == 0 )
	{
		// ��ײĿ�겻����Ϸ����㣬����
		if( !strstr( szSrcName, "putongyu" ) )
			return;

		// �ҵ�����Ķ���ʵ����������ײ��������
		CGameSprite	*pFish	=	GetFishFromName( szSrcName );
		if( NULL != pFish )
		{
			pFish->OnColControlFish((CMyFish*)GetControlFish() );
			// ��ײ֮�󣬲���˭��˭��������Ϸ�����ɾ����
			DeleteFishFromName( szSrcName );
		}
	}
	else
		return; // ��ײ���Ĳ�����ҿ��Ƶ���











}



void CGameMain::HandlePowerUpCollision(const char* szSrcName, const char* szTarName) {
    const char* szPowerUpName = strstr(szSrcName, "powerup_") ? szSrcName : szTarName;
    const char* szFishName = strcmp(szSrcName, GetControlFishName()) == 0 ? szSrcName : szTarName;

    if (strcmp(szFishName, GetControlFishName()) != 0)
        return;

    // ֱ��ͨ�������ж�����
    if (strstr(szPowerUpName, "jiaxie")) {
        // ��ѪЧ��
        if (m_pMyFish) {
            m_pMyFish->SetHp(m_pMyFish->GetHp() + 30);
        }
    }
    else if (strstr(szPowerUpName, "jiasu")) {
        // ����Ч��
        if (m_pMyFish) {
            m_pMyFish->SetSpriteLinearVelocityX(45.f); // ����50%
            m_pMyFish->SetSpriteLinearVelocityY(22.5f);
            m_fSpeedUpTime = 30.0f;
        }
    }
    else if (strstr(szPowerUpName, "wudi")) {
        // �޵�Ч��
        m_bIsGodMode = true;
        m_fGodModeTime = 30.0f;
    }

    // ɾ����ǿ����
    DeleteFishFromName(szPowerUpName);
}







//===========================================================================
//
// ����������߽���ײ
// ���� szName����ײ���߽�ľ�������
// ���� iColSide����ײ���ı߽� 0 ��ߣ�1 �ұߣ�2 �ϱߣ�3 �±�
void CGameMain::OnSpriteColWorldLimit( const char *szName, const int iColSide )
{
    // �˺�����������ҿ��Ƶ���������߽����ײ����Ϊ��Ϸ�����е��㶼�Ǵ��������ζ�
	// �������Ǹ���������һ������߽磬��ߵ�����߽����Ϸ�еĳ�����Ҫ����Щ�������γ�������������ߵı߽�֮�󣬽���ɾ��
	if( 0 == iColSide )
	{
		// �ж��ǲ��������봦������(������߰������������ַ�)
		if( strstr( szName, "putongyu" ) )
		{
			// ɾ��֮
			DeleteFishFromName( szName );
		}
    }

}
// ���������ȡһ������
CGameSprite* CGameMain::GetFishFromName( const char *szName )
{
	FishList	*pFish	=	NULL;
	for( pFish	=	m_pFishHeader; NULL != pFish; pFish	=	pFish->pNext )
	{
		if( strcmp( szName, pFish->pPtr->GetName() ) == 0 )
			return pFish->pPtr;
	}

	return NULL;
}

// ����һ�����鵽������
void CGameMain::AddFish( CGameSprite *pSprite )
{
	if( NULL == pSprite )
		return;
	FishList	*pFish	=	new FishList;
	//
	pFish->pPtr		=	pSprite;
	pFish->pNext	=	NULL;
	pFish->pPrev	=	NULL;
	// ����������
	if( NULL == m_pFishHeader )
		m_pFishHeader	=	pFish;
	else
	{
		FishList	*pTemp	=	m_pFishHeader;
		while( NULL != pTemp->pNext )
			pTemp	=	pTemp->pNext;

		pFish->pPrev	=	pTemp;
		pTemp->pNext	=	pFish;
	}
}

// ��������ɾ��һ�����飬ͬʱɾ���þ����ڵ�ͼ�е���ʾ
void CGameMain::DeleteFishFromName( const char *szName )
{
	FishList	*pFish	=	NULL;
	for( pFish	=	m_pFishHeader; NULL != pFish; pFish	=	pFish->pNext )
	{
		if( strcmp( szName, pFish->pPtr->GetName() ) == 0 )
		{
			// ����ָ���������ȡ��(���������е�ǰ��ָ������ָ��)
			// ����Ŀǰ�������£���ABC����ֵ��A <-> B <-> C����Ҫɾ��B
			// ����Ҫ��A��Nextָ��C��C��Prevָ��A��ɾ��֮����Ϊ A <-> C
			if( NULL != pFish->pNext )
			{
				pFish->pNext->pPrev	=	pFish->pPrev;
			}
			//
			if( NULL != pFish->pPrev )
			{
				pFish->pPrev->pNext	=	pFish->pNext;
			}

			// ����Ǳ�ͷ
			if( pFish == m_pFishHeader )
			{
				m_pFishHeader	=	m_pFishHeader->pNext;
			}

			// ɾ��Fish
			pFish->pPtr->DeleteSprite();
			// �ͷ��ڴ�
			delete pFish;
			//
			return;
		}
	}
}

// ɾ�����о��飬ͬʱɾ���þ����ڵ�ͼ�е���ʾ
void CGameMain::DeleteAllFish()
{
	FishList *pFish	=	NULL;
	FishList *pFishNext	=	m_pFishHeader;
	while( NULL != pFishNext )
	{
		pFish	=	pFishNext;
		pFishNext	=	pFishNext->pNext;

		// ɾ��Fish��ʾ
		pFish->pPtr->DeleteSprite();
		// �ͷ��ڴ�
		delete pFish;
	};

	m_pFishHeader =	NULL;
}


// CMyFish��

CMyFish::CMyFish(const char *szName,const int iHp, const int iLevel):CGameSprite( szName )
{
	m_iHp = iHp;
	m_iLevel =	iLevel;

	m_fVelocityLeft	=	0.f;
	m_fVelocityRight =	0.f;
	m_fVelocityUp =	0.f;
	m_fVelocityDown	 =	0.f;

	// �������ģ�ʹ�СΪ1/4
	SetSpriteScale( 0.25f );

	// ����߽�����
	SetSpriteWorldLimit( WORLD_LIMIT_STICKY,
		g_GameMain.GetWorldLeft() - 2.f, g_GameMain.GetWorldTop() - 2.f,
		g_GameMain.GetWorldRight() + 2.f, g_GameMain.GetWorldBottom() + 2.f );
}

CMyFish::~CMyFish()
{

}

void CMyFish::OnMove(bool bKeyDown,int iKey)
{
	if(bKeyDown)
	{
		switch(iKey)
		{
		case KEY_A:  // ��
			m_fVelocityLeft	 =	30.f;
			break;
		case KEY_D:  // ��
			m_fVelocityRight =	30.f;
			break;
		case KEY_W:  // ��
			m_fVelocityUp	 =	15.f;
			break;
		case KEY_S:  // ��
			m_fVelocityDown	 =	15.f;
			break;
		}
	}
	else
	{
		switch(iKey)
		{
		case KEY_A:  // ��
			m_fVelocityLeft	 =	0.f;
			break;
		case KEY_D:  // ��
			m_fVelocityRight =	0.f;
			break;
		case KEY_W:  // ��
			m_fVelocityUp	 =	0.f;
			break;
		case KEY_S:  // ��
			m_fVelocityDown	 =	0.f;
			break;
		}
	}

	float fVelX	= m_fVelocityRight - m_fVelocityLeft;
	float fVelY = m_fVelocityDown - m_fVelocityUp;

	SetSpriteLinearVelocity( fVelX,fVelY );

	// ���ٶȲ�Ϊ0��ʱ�򣬸�����ĳ���
	if( fVelX < -0.0001f || fVelX > 0.0001f )
	{
		if( fVelX > 0.0001f )
			SetSpriteFlipX( false );
		else
			SetSpriteFlipX( true );
	}
}


// CNormalFish��������

int	CGameSprite::m_iCreatedFishCount =	0;

CNormalFish::CNormalFish( const char *szName, const int iHp, const int iLevel ) : CGameSprite( szName )
{
	m_iHp = iHp;
	m_iLevel =	iLevel;
}

// ��ҿ��Ƶ�������ͨ����ײ�����еȼ��ж�
void CNormalFish::OnColControlFish( CMyFish *pMyFish )
{


    if (g_GameMain.IsGodMode())
        return;


	// ����ȼ�С�ڵ�����ҿ��Ƶ���ĵȼ���������ҿ��Ƶ����HP
	if( pMyFish->GetLevel() >= GetLevel() )
	{
		// ���Ƶ���HP����
		pMyFish->SetHp( pMyFish->GetHp() + GetHp() );

		// ������1������4�����ֱ���Ҫ��HP����Ϊ 200 1000 3000
		if( pMyFish->GetHp() >= 3000 && pMyFish->GetLevel() == 3 )
		{
			pMyFish->SetLevel( 4 );
			pMyFish->SetSpriteScale( 1.0f ); // ģ�ͱ��
		}
		else if( pMyFish->GetHp() >= 1000 && pMyFish->GetLevel() == 2 )
		{
			pMyFish->SetLevel( 3 );
			pMyFish->SetSpriteScale( 0.75f ); // ģ�ͱ��
		}
		else if( pMyFish->GetHp() >=200 && pMyFish->GetLevel() == 1 )
		{
			pMyFish->SetLevel( 2 );
			pMyFish->SetSpriteScale( 0.5f ); // ģ�ͱ��
		}
	}
	// ����ȼ����ڵ�����ҿ��Ƶ���ĵȼ���������ҿ��Ƶ����HP
	else
	{
		// ���Ƶ���HP����
		pMyFish->SetHp( pMyFish->GetHp() - GetHp() );

		  // ��ǰHPС�ڶ�Ӧ�ȼ���HP��������
	if( pMyFish->GetHp() <= 200 )
	{
		// ��һ�����۱���ȥ����HP������֮�󣬶���10��ʼ
		pMyFish->SetHp( 10 );
		pMyFish->SetLevel( pMyFish->GetLevel() - 1 );
		pMyFish->SetSpriteScale( 0.25f );
	}
	else if( pMyFish->GetHp() <= 1000 )
	{
		// ��һ�����۱���ȥ����HP������֮�󣬶���10��ʼ
		pMyFish->SetHp( 10 );
		pMyFish->SetLevel( pMyFish->GetLevel() - 1 );
		pMyFish->SetSpriteScale( 0.5f );

	}
	else if( pMyFish->GetHp() <= 3000 )
	{
		// ��һ�����۱���ȥ����HP������֮�󣬶���10��ʼ
		pMyFish->SetHp( 10 );
		pMyFish->SetLevel( pMyFish->GetLevel() - 1 );
		pMyFish->SetSpriteScale( 0.75f );

	}
	}
}

// CSpecialFish��������

CSpecialFish::CSpecialFish( const char *szName, const int iHp ) : CGameSprite( szName )
{
	m_iHp	=	iHp;
}

void CSpecialFish::OnColControlFish( CMyFish *pMyFish )
{

    if (g_GameMain.IsGodMode())
        return;


	// ���Ƶ���HP����
	pMyFish->SetHp( pMyFish->GetHp() - GetHp() );

	// ��ǰHPС�ڶ�Ӧ�ȼ���HP��������
	if( pMyFish->GetHp() <= 200 )
	{
		// ��һ�����۱���ȥ����HP������֮�󣬶���10��ʼ
		pMyFish->SetHp( 10 );
		pMyFish->SetLevel( pMyFish->GetLevel() - 1 );
		pMyFish->SetSpriteScale( 0.25f );
	}
	else if( pMyFish->GetHp() <= 1000 )
	{
		// ��һ�����۱���ȥ����HP������֮�󣬶���10��ʼ
		pMyFish->SetHp( 10 );
		pMyFish->SetLevel( pMyFish->GetLevel() - 1 );
		pMyFish->SetSpriteScale( 0.5f );

	}
	else if( pMyFish->GetHp() <= 3000 )
	{
		// ��һ�����۱���ȥ����HP������֮�󣬶���10��ʼ
		pMyFish->SetHp( 10 );
		pMyFish->SetLevel( pMyFish->GetLevel() - 1 );
		pMyFish->SetSpriteScale( 0.75f );

	}
}






// �޸�ChangeMap����
void CGameMain::ChangeMap(const char* szMapName)
{
    // ɾ�������㣨������ҿ��Ƶ��㣩
    DeleteAllFish();
    m_pMyFish = NULL;

    // �����µ�ͼ
    CSystem::LoadMap(szMapName);


    // ������Ϸ״̬
    m_iGameState = 0;
    m_bNeedReinit = true;

    // ����UI״̬��������
    ResetUIState();

    // ȷ����ʼ��
    GameInit();


}

// ������ʼ����鷽��
void CGameMain::CheckReinitialize()
{
    if (m_bNeedReinit)
    {
        GameInit();
        m_bNeedReinit = false;
    }
}
// �޸�HandleMapChangeClick
void CGameMain::HandleMapChangeClick(const float fMouseX, const float fMouseY)
{
    CSprite* pSprite = new CSprite("shengji");
    if (pSprite->IsPointInSprite(fMouseX, fMouseY))
    {
        ChangeMap("level1.t2d");
        delete pSprite;
        return;
    }
    delete pSprite;

    pSprite = new CSprite("taosheng");
    if (pSprite->IsPointInSprite(fMouseX, fMouseY))
    {
        ChangeMap("level2.t2d");
        delete pSprite;
        return;
    }
    delete pSprite;

    pSprite = new CSprite("fanhui1");
    if (pSprite->IsPointInSprite(fMouseX, fMouseY))
    {
        ChangeMap("level0.t2d");
        delete pSprite;
        return;
    }
    delete pSprite;

    pSprite = new CSprite("fanhui2");
    if (pSprite->IsPointInSprite(fMouseX, fMouseY))
    {
        ChangeMap("level0.t2d");
        delete pSprite;
        return;
    }
    delete pSprite;
}
// ��������ʵ��
void CGameMain::ResetUIState()
{
    // ���´���UIԪ�أ����ָ��Ϊ�գ�
    if (!m_pLevelText) m_pLevelText = new CTextSprite("LevelText");
    if (!m_pHPText) m_pHPText = new CTextSprite("HPText");
    if (!m_pBeginSprite) m_pBeginSprite = new CTextSprite("kaishi");
    if (!m_pBeginSpritePlus) m_pBeginSpritePlus = new CTextSprite("kaishibiaoti");
    if (!m_pJieShuSprite) m_pJieShuSprite = new CTextSprite("jieshu");

    // ���ݵ�ǰ��Ϸ״̬����UI�ɼ���
    if (m_iGameState == 0) // �ȴ���ʼ״̬
    {
        m_pBeginSprite->SetSpriteVisible(true);
        m_pBeginSpritePlus->SetSpriteVisible(true);
        m_pJieShuSprite->SetSpriteVisible(false);
    }
    else // ��Ϸ�����л����״̬
    {
        m_pBeginSprite->SetSpriteVisible(false);
        m_pBeginSpritePlus->SetSpriteVisible(false);
    }
}
// 新增函数实现 - 生成jianyou精灵
void CGameMain::CreateJianYou(float fDeltaTime)
{
    m_fCreateJianYouTime -= fDeltaTime;

    if (m_fCreateJianYouTime <= 0.0f)
    {
        m_fCreateJianYouTime = m_fJianInterval;

        // 获取"renleiyou"精灵位置
        CSprite* pRenLeiYou = new CSprite("renleiyou");
        if (pRenLeiYou)
        {
            float fPosX = pRenLeiYou->GetSpritePositionX();
            float fPosY = pRenLeiYou->GetSpritePositionY();

            // 创建"jianyou"精灵
            char szName[128];
            sprintf(szName, "jianyou_%d", CGameSprite::m_iCreatedFishCount++);
            CGameSprite* pJianYou = new CGameSprite(szName);
            pJianYou->CloneSprite("jianyou");
            pJianYou->SetSpritePosition(fPosX, fPosY);
            pJianYou->SetSpriteLinearVelocityX(28.0f); // 向右匀速运动
            pJianYou->SetSpriteWorldLimit(WORLD_LIMIT_KILL,
                GetWorldLeft(), GetWorldTop(),
                GetWorldRight(), GetWorldBottom());

            AddFish(pJianYou);
        }
        delete pRenLeiYou;
    }
}

// 新增函数实现 - 生成jianzuo精灵
void CGameMain::CreateJianZuo(float fDeltaTime)
{
    m_fCreateJianZuoTime -= fDeltaTime;

    if (m_fCreateJianZuoTime <= 0.0f)
    {
        m_fCreateJianZuoTime = m_fJianInterval;

        // 获取"renleizuo"精灵位置
        CSprite* pRenLeiZuo = new CSprite("renleizuo");
        if (pRenLeiZuo)
        {
            float fPosX = pRenLeiZuo->GetSpritePositionX();
            float fPosY = pRenLeiZuo->GetSpritePositionY();

            // 创建"jianzuo"精灵
            char szName[128];
            sprintf(szName, "jianzuo_%d", CGameSprite::m_iCreatedFishCount++);
            CGameSprite* pJianZuo = new CGameSprite(szName);
            pJianZuo->CloneSprite("jianzuo");
            pJianZuo->SetSpritePosition(fPosX, fPosY);
            pJianZuo->SetSpriteLinearVelocityX(-30.0f); // 向右匀速运动
            pJianZuo->SetSpriteWorldLimit(WORLD_LIMIT_KILL,
                GetWorldLeft(), GetWorldTop(),
                GetWorldRight(), GetWorldBottom());

            AddFish(pJianZuo);
        }
        delete pRenLeiZuo;
    }
}


// 新增碰撞处理函数实现
void CGameMain::HandleKongzhiyu2Collision(const char* szSrcName, const char* szTarName)
{
    // 检查是否是kongzhiyu2与baoxiang1的碰撞
    bool bKongzhiyu2Collided = (strstr(szSrcName, "kongzhiyu2") || strstr(szTarName, "kongzhiyu2"));
    bool bBaoxiang1Collided = (strstr(szSrcName, "baoxiang1") || strstr(szTarName, "baoxiang1"));

    if(bKongzhiyu2Collided && bBaoxiang1Collided)
    {
        // 确定baoxiang1的名字
        const char* szBaoxiangName = strstr(szSrcName, "baoxiang1") ? szSrcName : szTarName;

        // 删除宝箱
        DeleteFishFromName(szBaoxiangName);


    }
}




